import java.util.Stack;

/**
 * Created by cc
 * Description: 左右括号问题
 * User: CZH
 * Date: 2022-09-28
 * Time: 18:43
 */
public class Test02 {

    //左右括号问题
    public static boolean isValid1(String s) {
        if (s == null || s.equals("")) {
            return true;
        }
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '{' || chars[i] == '[' || chars[i] == '(') {
                stack.push(chars[i]);
            }
            if (stack.size() != 0){
                if (chars[i] == '}') {
                    if (stack.peek() == '{') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
                if (chars[i] == ']') {
                    if (stack.peek() == '[') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
                if (chars[i] == ')') {
                    if (stack.peek() == '(') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }else {
                return false;
            }
        }
        if (stack.size() != 0) {
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        String s = "[";
        System.out.println(isValid1(s));
    }
}
